package com.bianmaba.spring.supports.configure;

import com.bianmaba.spring.supports.data.repository.ExtendedRepositoryImpl;
import com.bianmaba.spring.supports.mvc.method.EntityParamListMethodArgumentResolver;
import com.bianmaba.spring.supports.mvc.method.EntityParamMethodArgumentResolver;
import com.bianmaba.spring.supports.mvc.method.MapParamMethodArgumentResolver;
import com.bianmaba.spring.supports.mvc.method.StrutsJsonHandlerMethodReturnValueHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
@ComponentScan(basePackages = {"com.bianmaba.spring.supports"})
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
public class SpringBootSupportsAutoConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public StrutsJsonHandlerMethodReturnValueHandler strutsJsonHandlerMethodReturnValueHandler() {
        return new StrutsJsonHandlerMethodReturnValueHandler();
    }

    @Bean
    public EntityParamListMethodArgumentResolver entityParamListMethodArgumentResolver() {
        return new EntityParamListMethodArgumentResolver();
    }

    @Bean
    public EntityParamMethodArgumentResolver entityParamMethodArgumentResolver() {
        return new EntityParamMethodArgumentResolver();
    }


    @Bean
    public MapParamMethodArgumentResolver mapParamMethodArgumentResolver() {
        return new MapParamMethodArgumentResolver();
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(strutsJsonHandlerMethodReturnValueHandler());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(entityParamListMethodArgumentResolver());
        argumentResolvers.add(entityParamMethodArgumentResolver());
        argumentResolvers.add(mapParamMethodArgumentResolver());
    }
}
